using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 加载预制体服务
/// </summary>
public class LoadPrefabService : ILoadPrefabListener
{
    public static LoadPrefabService Instance { get; private set; } = new LoadPrefabService();

    private Contexts _contexts;

    private Transform _settleParent;
    private Transform _movableParent;

    public void Init(Contexts contexts, Transform gameController)
    {
        _contexts = contexts;
        contexts.game.CreateEntity().AddLoadPrefabListener(this);
        CreateSubParent(gameController);
    }

    private void CreateSubParent(Transform parent)
    {
        _settleParent = new GameObject("Settle").transform;
        _settleParent.SetParent(parent);
        _movableParent = new GameObject("Movable").transform;
        _movableParent.SetParent(parent);
    }

    public void OnLoadPrefab(GameEntity entity, string path)
    {
        Transform parent;
        if (entity.isMovable)
        {
            parent = _movableParent;
        }
        else
        {
            parent = _settleParent;
        }

        GameObject go = Resources.Load<GameObject>(path);
        var view = Object.Instantiate(go, parent).GetComponent<IView>();
        view.Link(entity, _contexts.game);
    }
}